package com.kaili.controller.common.v1;

import com.baomidou.mybatisplus.plugins.Page;
import com.kaili.common.util.LoginDetailUtils;
import com.kaili.db.service.IBaseCarService;
import com.kaili.db.service.IHsOptArriveService;
import com.kaili.except.ResponseStatus;
import com.kaili.pojo.dto.arrive.ArriveQueryDTO;
import com.kaili.pojo.dto.arrive.ArriveStowageDTO;
import com.kaili.pojo.entity.BaseCar;
import com.kaili.pojo.vo.ArriveEwbVO;
import com.kaili.pojo.vo.StowageVO;
import com.kaili.version.ApiVersion;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @AUTHOR pengdongxu.
 * @Date: 18-5-14 上午9:13.
 * @COPYRIGHT 杭州凯立
 */

@ApiVersion(1)
@RequestMapping("/{api_version}")
@Validated
@Api(tags = "[PDA和WEB端通用]到件服务", description = "[通用]到件服务API")
@RestController("arriveBasicControllerC")
public class ArriveBasicController {
    private static final Logger logger = LogManager.getLogger();

    @Autowired
    private IHsOptArriveService hsOptArriveService;
    @Autowired
    private IBaseCarService carService;

    @GetMapping("/c/arrives")
    @ApiOperation("获取配载单在途/到货信息")
    public Page<StowageVO> getPageList(@Validated @ModelAttribute ArriveQueryDTO req){
        if (req.getCur()!=null && req.getPageSize()!=null){
            Page<StowageVO> pager = new Page<>(req.getCur(), req.getPageSize());
            Map<Object, Object> search = new HashMap<>();
            search.putAll(new org.apache.commons.beanutils.BeanMap(req));
            search.put("destinationSiteId",LoginDetailUtils.getSiteId());
            Page<ArriveEwbVO> pagerEwb = new Page<>(req.getCur(), req.getPageSize());
            try {
                pager = hsOptArriveService.queryStowageArrive(pager, search);
                //根据数据来源 1 web 2pda
                Integer scanSourceId = (Integer) search.get("sourceType");
                if(null!=scanSourceId&&scanSourceId==2){
                    List<StowageVO> records = pager.getRecords();
                    for (StowageVO stowageVO:records) {
                        Map<String,Object> map=new HashMap<>();
                        map.put("operationId",stowageVO.getOperationId());
                        List<ArriveEwbVO> ewlist = hsOptArriveService.queryArriveEwb(pagerEwb, map).getRecords();
                        stowageVO.setArriveEwbVO(ewlist);
                    }
                }
            } catch (Exception e) {
                logger.info("获取配载单在途/到货信息-发生异常",e);
            }
            return pager;
        }

        return null;
    }

    @ApiOperation("B/S配载单到件")
    @PostMapping("/c/arrives/bat")
    public ResponseStatus doStowageArriveConfirm(@RequestBody List<ArriveStowageDTO> stowage) {
        boolean flag = hsOptArriveService.doStowageArriveConfirm(stowage);
        if (!flag){
            return ResponseStatus.ADD_FAIL;
        }
        BaseCar baseCar;
        for(ArriveStowageDTO stowageDTO : stowage){
            baseCar = new BaseCar();
            baseCar.setId(stowageDTO.getCarId());
            baseCar.setCarStatus("空闲");
            carService.updateById(baseCar);
        }
        return ResponseStatus.RESPONSE_OK;
    }
}
